package com.gdufs.secondhandwebsite.controller;

import com.gdufs.secondhandwebsite.entity.Comment;
import com.gdufs.secondhandwebsite.service.CommentService;
import com.gdufs.secondhandwebsite.service.CommentService;
import com.gdufs.secondhandwebsite.util.JsonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;


@Api(tags = "二手交易网站用户评论类相关接口")
@RestController
@RequestMapping("comment")
public class CommentController extends BaseController{
    @Autowired
    CommentService commentService;

    @ApiOperation(value = "创建评论接口", notes = "需要传入评论的内容")
    @PostMapping("/create")
    public JsonResult<Long> createComment(@RequestParam("content")String content, HttpSession httpSession,@RequestParam("postId")Long postId) {
//        获取当前发布评论的用户id
        System.out.println("content"+content);
        System.out.println("postId"+postId);
        Comment comment = new Comment();
        comment.setContent(content);
        System.out.println(comment);
        String userName = getUsernameFromSession(httpSession);
        String userImg =getUserImgFromSession(httpSession);

        commentService.createComment(comment,userName,userImg,postId);
        System.out.println("新增评论id："+comment.getCommentId());
        return new JsonResult<Long>(OK,comment.getCommentId());
    }

    @ApiOperation(value = "更新评论内容接口", notes = "可选择性地更新评论内容的任意字段")
    @PostMapping("/update")
    public JsonResult<Comment> updateComment(@RequestBody Comment comment){
        boolean is_update =commentService.updateComment(comment);
        JsonResult jsonResult = new JsonResult<Comment>(OK);
        jsonResult.setData(comment);
        if(is_update){
            jsonResult.setMessage("评论更新成功");
        }else {
            jsonResult.setMessage("评论更新失败");
        }
        return jsonResult;
    }

    @ApiOperation(value = "删除评论接口", notes = "根据评论Id删除评论")
    @DeleteMapping("{id}/delete")
    public JsonResult<Void> deleteById(@PathVariable("id") Integer id) {
        // 调用业务对象执行获取数据
        boolean is_del = commentService.delComment(id.longValue());
        JsonResult jsonResult = new JsonResult<Comment>(OK);
        if(is_del){
            jsonResult.setMessage("评论删除成功");
        }else {
            jsonResult.setMessage("评论删除失败");
        }
        return jsonResult;
    }

    @ApiOperation(value = "获取所有评论接口")
    @GetMapping("all")
    public JsonResult<List<Comment>> getCommentAll(){
        List<Comment> result =commentService.getAllComment();
        JsonResult jsonResult = new JsonResult<List<Comment>>(OK);
        jsonResult.setData(result);
        return  jsonResult;
    }

    @ApiOperation(value = "评论展示接口", notes = "根据帖子" +
            "Id展示该帖子的所有评论")
    @PostMapping("/select")
    public JsonResult<List<Comment>> getCommentByPostId(Long postId){
        List<Comment> result =commentService.getCommentByPostId(postId);
        JsonResult jsonResult = new JsonResult<List<Comment>>(OK);
        jsonResult.setData(result);
        return  jsonResult;
    }
}
